<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { DeleteData, GetData } from '@/api/modules/memberlevels'
import PageNumber from '@/store/modules/PageNumberCache'

const imgDomain = 'https://img1.eprhan.cc'
const router = useRouter()
const PageNumberCache = PageNumber()

const findFrom = ref({
  Province: '',
})
const dataList: any = reactive({
  list: [],
  option: [],
})
const loading = ref(true)
const total = ref(0)
let PageSize = 20

async function GetDataList(PageIndex: number = 1, PageSize: number = 20) {
  const params = {
    PageIndex,
    PageSize,
    ...(findFrom.value.Province && ({ Province: findFrom.value.Province })),
  }
  await GetData(params).then(async (dataRes: any) => {
    if (dataRes.succeeded) {
      dataList.list = dataRes.data
      total.value = dataRes.data.total
      loading.value = false
    }
  })
  loading.value = false
}
// // 查询
// function Inquire() {
//   GetDataList()
// }

// 添加
function AddData() {
  router.push({
    name: 'memberlevelsDetail',
  })
}
// 编辑
function EditData(id: number) {
  router.push({
    name: 'memberlevelsDetail',
    query: {
      id,
    },
  })
}

function Delete(id: string) {
  ElMessageBox.confirm(
    '您确定要删除吗?',
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    },
  )
    .then(() => {
      DeleteData(id).then((res: any) => {
        if (res.succeeded) {
          GetDataList(PageNumberCache.GetMemberLevelsPage(), PageSize)
          ElMessage({
            type: 'success',
            message: '删除成功',
          })
        }
        else {
          ElMessage.error(`删除失败${res.errors}`)
        }
      })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消删除',
      })
    })
}

// 翻页
function handleSizeChange(val: number) {
  GetDataList(1, val)
  PageSize = val
}
function handleCurrentChange(val: number) {
  GetDataList(val, PageSize)
  PageNumberCache.SetMemberLevelsPage(val)
}

const nowPageNum = computed(() => {
  return PageNumberCache.GetMemberLevelsPage()
})

onMounted(() => {
  GetDataList(PageNumberCache.GetMemberLevelsPage())
})
</script>

<template>
  <div>
    <PageMain>
      <SearchBar :show-toggle="false">
        <!-- <div class="header">
          <el-form label-positio="right" label-width="auto">
            <div class="from">
              <el-form-item label="地区">
                <el-input v-model="findFrom.Province" placeholder="请输入地区" clearable />
              </el-form-item>
            </div>
          </el-form>
          <div class="text-right">
            <el-button @click="Inquire">
              查询
            </el-button>
          </div>
        </div> -->
      </SearchBar>
      <ElDivider border-style="dashed" />
      <div class="mb-5 text-right">
        <el-button type="success" @click="AddData">
          添加
        </el-button>
      </div>
      <el-table v-loading="loading" :data="dataList.list" stripe style="width: 100%;" border>
        <el-table-column align="center" prop="id" label="ID" width="100" />
        <el-table-column align="center" prop="vipLevel" label="会员等级" width="220" />
        <el-table-column align="center" prop="vipName" label="会员等级名称" width="220" />
        <el-table-column align="center" prop="vipIntegralLimit" label="会员等级积分要求" width="160" />
        <el-table-column align="center" prop="vipCard" label="会员卡面" width="140">
          <template #default="scope">
            <el-avatar shape="square" :size="80" :src="imgDomain + scope.row.vipCard" />
          </template>
        </el-table-column>
        <el-table-column align="center" prop="vipLogo" label="会员徽标" width="140">
          <template #default="scope">
            <el-avatar shape="square" :size="80" :src="imgDomain + scope.row.vipLogo" />
          </template>
        </el-table-column>
        <el-table-column align="center" prop="vipWidget" label="会员头饰挂件" width="140">
          <template #default="scope">
            <el-avatar shape="square" :size="80" :src="imgDomain + scope.row.vipWidget" />
          </template>
        </el-table-column>
        <el-table-column align="center" prop="addTime" label="添加时间" width="200" />
        <el-table-column align="center" prop="updateTime" label="更新时间" width="200" />
        <!-- <el-table-column align="center" prop="flag" label="类型" width="100">
          <template #default="scope">
            <el-tag>
              {{ scope.row.flag === 1 ? '通用模板'
                : scope.row.flag === 2 ? '顺丰模板'
                  : '未知' }}
            </el-tag>
          </template>
        </el-table-column> -->
        <el-table-column align="center" label="操作" fixed="right" min-width="180">
          <template #default="scope">
            <el-button type="primary" @click="EditData(scope.row.id)">
              编辑
            </el-button>
            <el-button type="danger" @click="Delete(scope.row.id)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="height: 10px;" />
      <el-pagination
        :default-page-size="20"
        :page-sizes="[5, 10, 20, 50, 100]"
        layout="total, sizes, ->, prev, pager, next, jumper"
        :total="total"
        :default-current-page="nowPageNum"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </PageMain>
  </div>
</template>

<style scoped>
.header {
  display: grid;
  grid-template-columns: 6fr 1fr;

  .from {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    grid-gap: 20px;

    .el-form-item {
      margin-bottom: 0;
    }
  }
}
</style>
